package com.robot.controller;

import com.robot.model.BuildTestCaseDo;
import com.robot.model.BuildTestDo;
import com.robot.service.BuildTestService;
import com.robot.util.SessionUtil;
import com.robot.vo.ResponseMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: li
 * Date: 2019/12/8
 * Time: 11:09
 * PROJECT：robot
 * PACKAGE: com.robot
 *
 * @author:55324936@qq.com To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/report")
@Slf4j
public class ReportController {

    @Autowired
    BuildTestService buildTestService;

    @RequestMapping("/reportPage")
    public ModelAndView report(Integer buildTestId) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/report/report");
        modelAndView.addObject("buildTestId", buildTestId);
        return modelAndView;
    }
    @RequestMapping("/reportDetailPage")
    public ModelAndView reportDetailPage(Integer buildTestInterfaceId) {
        ModelAndView modelAndView = new ModelAndView();
        List<BuildTestCaseDo> buildTestCaseDoList= buildTestService.buildTestCaseDoList(buildTestInterfaceId);
        modelAndView.setViewName("/report/reportDetail");
        modelAndView.addObject("buildTestCaseList", buildTestCaseDoList);
        return modelAndView;
    }

    @RequestMapping("/getBuildTest")
    public @ResponseBody
    ResponseMessage getBuildTest(Integer buildTestId) {
        try {
            BuildTestDo buildTestDo = buildTestService.buildRunTest(buildTestId);
            return ResponseMessage.successResponse(buildTestDo);
        } catch (Exception e) {
            log.error("获取运行的构建信息异常", e);
            return ResponseMessage.errorResponse("");
        }

    }
    @RequestMapping("/lastBuild")
    public @ResponseBody
    ResponseMessage lastBuild() {
       Integer projectId =SessionUtil.getSessionUser().getLastProjectId();
        try {
            BuildTestDo buildTestDo = buildTestService.lastBuild(projectId);
            return ResponseMessage.successResponse(buildTestDo);
        } catch (Exception e) {
            log.error("获取运行的构建信息异常", e);
            return ResponseMessage.errorResponse("");
        }

    }
    @RequestMapping("/getBuildTestRightMenu")
    public @ResponseBody ResponseMessage buildTestRightMenu(){
        try {
            Integer projectId = SessionUtil.getSessionUser().getLastProjectId();
            List<BuildTestDo> buildTestDoList= buildTestService.lastBuildTestByProjectId(projectId);
            return ResponseMessage.successResponse(buildTestDoList);
        }catch (Exception e){
            log.error("获取历史执行异常",e);
            return ResponseMessage.errorResponse("获取历史执行记录异常");
        }
    }
}
